package com.chenyi.leetcode.editor.cn;

/**
 * [26]删除有序数组中的重复项
 * @author by chenyi
 * @className RemoveDuplicatesFromSortedArray
 * @date 2022-06-20 21:10:46
 */
public class RemoveDuplicatesFromSortedArray{
    public static void main(String[] args) {
        Solution solution = new RemoveDuplicatesFromSortedArray().new Solution();
        solution.removeDuplicates(new int[]{1, 1, 2});
    }
    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int removeDuplicates(int[] nums) {
//        int count = 0;
//        for (int i = 0; i < nums.length; i++) {
//            if (i == 0 || i + 1 < nums.length && nums[i] != nums[i + 1]) {
//                nums[count++] = nums[i];
//            }
//        }
//        System.out.println(count);
//        return count;
        int n = nums.length;
        if (n == 0) {
            return 0;
        }
        int fast = 1, slow = 1;
        while (fast < n) {
            if (nums[fast] != nums[fast - 1]) {
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        System.out.println(slow);
        return slow;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
